//
//  WPhotoShowCell.m
//  PhotosBrowser
//
//  Created by winter on 2016/11/2.
//  Copyright © 2016年 JHJR. All rights reserved.
//

#import "WPhotoShowCell.h"
#import "WPhotoShowView.h"
#import "WPhotoModel.h"
#import "WPhotoBrowserConst.h"

@implementation WPhotoShowCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 创建contentScrollView
        UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
        // 取消滑动指示器
        contentScrollView.showsVerticalScrollIndicator = NO;
        contentScrollView.showsHorizontalScrollIndicator = NO;
        self.contentScrollView = contentScrollView;
        [self.contentView addSubview:contentScrollView];

        [self.contentScrollView addSubview:self.photoShowView];
    }
    return self;
}

- (WPhotoShowView *)photoShowView
{
    if (!_photoShowView) {
        _photoShowView = [[WPhotoShowView alloc] init];
        _photoShowView.photoCell = self;
    }
    return _photoShowView;
}

- (void)setActionBlock:(void (^)(PhotoViewActionType))actionBlock
{
    self.photoShowView.actionBlock = actionBlock;
}

- (void)setPhotoModel:(WPhotoModel *)photoModel
{
    _photoModel = photoModel;
    
    self.photoShowView.photoModel = photoModel;
    
    // 取出图片大小
    CGSize imageSize = self.photoShowView.image.size;
    CGFloat width = self.contentScrollView.width;
    
    self.photoShowView.size = CGSizeMake(width, width * imageSize.height / imageSize.width);
    
    if (self.width > self.height) {
        // 横屏
        CGFloat height = 0;
        if (photoModel.originalSize.width > photoModel.originalSize.height * 2) {
            // （原始图片）宽大于高的两倍
            height = WScreenH;
            width = height * photoModel.originalSize.height / photoModel.originalSize.width;
        } else {
            // （原始图片）高大于宽
            height = WScreenW * photoModel.originalSize.width / photoModel.originalSize.height > WScreenH ? WScreenH : WScreenW * photoModel.originalSize.width / photoModel.originalSize.height;
        }
        self.photoShowView.size = CGSizeMake(height, width);
    }
    
    photoModel.originalSize = self.photoShowView.size;
    photoModel.verticalWidth = photoModel.originalSize.width;
    
    self.photoShowView.center = CGPointMake(self.contentScrollView.width * 0.5, self.contentScrollView.height * 0.5);
    if (imageSize.height > WScreenH) {
        self.photoShowView.y = 0;
    }
    [self.contentScrollView setContentSize:self.photoShowView.size];
}
@end
